package com.hzit.ms.customer;

import com.hzit.ms.controller.GoodsController;
import com.hzit.ms.service.IOrderService;
import com.hzit.ms.vo.CreateOrderReqData;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 创建秒杀订单的消费者
 */
@Component
@RabbitListener(bindings = @QueueBinding(
        //指定绑定的队列（该队列将自动创建），autoDelete="true"意味着程序关闭时该队列自动删除
        value = @Queue(value = "ms.queue",autoDelete = "true"),
        //指定交换机，交换机类型：topic
        exchange = @Exchange(value = "ms.exchange.topic",type = ExchangeTypes.TOPIC),
        //队列与交换机绑定的路由键，带有此路由键的消息会被交换机发送到该队列
        key = "create.*"
))
public class SkCreateOrderCustomer {

    private Logger logger = LoggerFactory.getLogger(SkCreateOrderCustomer.class);

    @Autowired
    private IOrderService orderService;

    /**
     * 处理逻辑
     */
    @RabbitHandler
    public void process(CreateOrderReqData createOrderReqData, Channel channel, Message msg){

        logger.info("接收到队列中创建订单的请求，请求参数：{}",createOrderReqData);
        //生成秒杀的订单
        orderService.createOrder(createOrderReqData);

    }
}
